package org.jeecg.modules.demo.DingTalk.MonthPayableOnoa.service.impl;

import org.jeecg.modules.demo.DingTalk.MonthPayableOnoa.entity.MonthPayableOnoa;
import org.jeecg.modules.demo.DingTalk.MonthPayableOnoa.entity.MonthPayableOnoaItem;
import org.jeecg.modules.demo.DingTalk.MonthPayableOnoa.mapper.MonthPayableOnoaItemMapper;
import org.jeecg.modules.demo.DingTalk.MonthPayableOnoa.mapper.MonthPayableOnoaMapper;
import org.jeecg.modules.demo.DingTalk.MonthPayableOnoa.service.IMonthPayableOnoaService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 钉钉月结汇总单
 * @Author: jeecg-boot
 * @Date:   2025-11-19
 * @Version: V1.0
 */
@Service
public class MonthPayableOnoaServiceImpl extends ServiceImpl<MonthPayableOnoaMapper, MonthPayableOnoa> implements IMonthPayableOnoaService {

	@Autowired
	private MonthPayableOnoaMapper monthPayableOnoaMapper;
	@Autowired
	private MonthPayableOnoaItemMapper monthPayableOnoaItemMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(MonthPayableOnoa monthPayableOnoa, List<MonthPayableOnoaItem> monthPayableOnoaItemList) {
		monthPayableOnoaMapper.insert(monthPayableOnoa);
		if(monthPayableOnoaItemList!=null && monthPayableOnoaItemList.size()>0) {
			for(MonthPayableOnoaItem entity:monthPayableOnoaItemList) {
				//外键设置
				entity.setMainId(monthPayableOnoa.getId());
				monthPayableOnoaItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(MonthPayableOnoa monthPayableOnoa,List<MonthPayableOnoaItem> monthPayableOnoaItemList) {
		monthPayableOnoaMapper.updateById(monthPayableOnoa);
		
		//1.先删除子表数据
		monthPayableOnoaItemMapper.deleteByMainId(monthPayableOnoa.getId());
		
		//2.子表数据重新插入
		if(monthPayableOnoaItemList!=null && monthPayableOnoaItemList.size()>0) {
			for(MonthPayableOnoaItem entity:monthPayableOnoaItemList) {
				//外键设置
				entity.setMainId(monthPayableOnoa.getId());
				monthPayableOnoaItemMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		monthPayableOnoaItemMapper.deleteByMainId(id);
		monthPayableOnoaMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			monthPayableOnoaItemMapper.deleteByMainId(id.toString());
			monthPayableOnoaMapper.deleteById(id);
		}
	}
	
}
